/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entities.Staff;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author AMENOSA
 */
public class StaffModel extends MyConfig {

    public StaffModel() {
    }

    public Iterator getAllStaff() {
        try {
            openConnect();
            ArrayList list = new ArrayList();
            CallableStatement pst = conn.prepareCall("{call }");
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                Staff staff = new Staff();
                staff.setId(rs.getInt("Staff_id"));
                staff.setDepartment_id(rs.getInt("Department_id"));
                staff.setName(rs.getString("Staff_name"));
                staff.setPhone(rs.getString("Staff_phone"));
                staff.setEmail(rs.getString("Staff_email"));
                staff.setDob(rs.getDate("Staff_dob"));
                staff.setDate_join(rs.getDate("Date_joined"));
                staff.setDate_left(rs.getDate("Date_left"));
                staff.setAcc_role_id(rs.getInt("Acc_role_id"));
                staff.setUsername("Username");
                staff.setPassword("[Password]");
                staff.setStatus(rs.getBoolean("Status"));
                list.add(staff);
            }
            closeConnect();
            return list.iterator();
        } catch (SQLException ex) {
        }
        return null;
    }

    public Staff checkLogin(String userName, String passWord) {
        Staff staff = null;
        try {
            openConnect();
            CallableStatement cst = conn.prepareCall("{call checkLogin(?,?)}");
            cst.setString(1, userName);
            cst.setString(2, passWord);
            ResultSet rs = cst.executeQuery();
            if (rs.next()) {
                staff = new Staff();
                staff.setId(rs.getInt("Staff_id"));
                staff.setDepartment_id(rs.getInt("Department_id"));
                staff.setName(rs.getString("Staff_name"));
                staff.setPhone(rs.getString("Staff_phone"));
                staff.setEmail(rs.getString("Staff_email"));
                staff.setDob(rs.getDate("Staff_dob"));
                staff.setDate_join(rs.getDate("Date_joined"));
                staff.setDate_left(rs.getDate("Date_left"));
                staff.setAcc_role_id(rs.getInt("Acc_role_id"));
                staff.setUsername("Username");
                staff.setPassword("[Password]");
                staff.setStatus(rs.getBoolean("Status"));
            }
            closeConnect();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return staff;
    }

    public int getPageCount(int pageSize) {
        int result = 0, count = 0;
        try {
            openConnect();
            PreparedStatement pst = conn.prepareCall("");
            ResultSet rs = pst.executeQuery();
            if (rs.next()) {
                count = rs.getInt("Count");
            }
            closeConnect();
            int temp = count % pageSize;
            if (temp != 0) {
                result = count / pageSize + 1;
            } else {
                result = count / pageSize;
            }
        } catch (SQLException ex) {
        }
        return result;
    }
}
